package myutil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionTest
{
    public static void main(String[] args)
    {
        try
        {
            // 1、定义目标地址
           String url = "http://localhost:8089/face_web_war_exploded/ajax";
          //  String url ="https://www.baidu.com/";
            // 2、与目标地址建立连接
            URL connectionUrl = new URL(url);
            System.out.println(url);
            HttpURLConnection urlConnection = (HttpURLConnection) connectionUrl.openConnection();
            // 3、获取服务端的数据，只有调用该方法才会向服务端发起请求
            urlConnection.getInputStream();
            // 获取服务端的数据
            InputStream inputStream = urlConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            String result = "";
            while ((line = bufferedReader.readLine()) != null)
            {
                result += line;
            }
            System.out.println(result);
        }catch (Exception e)
        {
            e.printStackTrace();

        }
    }
}
